package com.jz.yanshe.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.jz.jd.rest.response.ConfigResponse;
import com.jz.yanshe.base.BaseDialogFragment;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.AreaResponse;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.jz.yanshe.R;
import com.jz.yanshe.ui.HomeActivity;
import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2017/11/23.
 */

public class UpdateFragment extends BaseDialogFragment {

    public static UpdateFragment newInstance() {
        return new UpdateFragment();
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView btn_cancel, btn_confirm, tv_update1, tv_update2, tv_update3;
    private TextView[] textViews = new TextView[3];

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_update, container);
        tv_update1 = view.findViewById(R.id.tv_update1);
        tv_update2 = view.findViewById(R.id.tv_update2);
        tv_update3 = view.findViewById(R.id.tv_update3);
        textViews[0] = tv_update1;
        textViews[1] = tv_update2;
        textViews[2] = tv_update3;
        btn_cancel = view.findViewById(R.id.btn_cancel);
        btn_confirm = view.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onConfirm();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        getConfigList();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
//            window.setGravity(Gravity.BOTTOM);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    private void getConfigList() {
        RestProxy.getInstance().getConfigList(PreferencesHelper.getInstance().getString(getActivity(), "token"), "14", new Observer<ConfigResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ConfigResponse configResponse) {
                for (int i = 0; i < configResponse.list.get(2).common_configuration_description.split(",").length && i < 3; i++) {
                    textViews[i].setText(configResponse.list.get(2).common_configuration_description.split(",")[i]);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


}
